﻿<?php
define('ROOT', '../');
include_once (ROOT . 'includes/common.php');

if (!iflogin(DBQZ,$userrow['cookie'])) {
}else{
	header("Location: main.php"); 
}
if(defense($_POST['from'])=='reg'){
	$user 		= defense($_POST['user']);
	$pwd 		= defense($_POST['pwd']);
	$mail 		= defense($_POST['mail']);
	$qq 		= is_numeric($_POST['qq'])?defense($_POST['qq']):'';
	$code 		= defense(trim($_POST['code']));
	$ip			= real_ip();
	$pattern	= "/([a-z0-9]*[-_.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[.][a-z]{2,3}([.][a-z]{2})?/i";
	$date 		= date("Y-m-d H:i:s");
	$pwd_md5 	= md5($pwd);
	if($user=='' && $pwd=='' && $qq=='' && $mail==''){
		$arr = array(
			'code' => 0,
			'msg' => '必填选项不能留空',
		);
	}elseif(strtolower($code)!= $_SESSION["helloweba_code"]){
	    $arr = array(
			'code' => 2,
			'msg' => '验证码错误',
		);
    }elseif(strlen($user) < 4){
		$arr = array(
			'code' => 0,
			'msg' => '账户名长度小于4位',
		);
	}elseif(strlen($pwd) < 6){
		$arr = array(
			'code' => 0,
			'msg' => '密码长度小于6位',
		);
	}elseif($DB->count("select count(uid) as count from " . DBQZ . "_user where `regip` ='$ip'") > intval($conf['reglimit'])){
		$arr = array(
			'code' => 0,
			'msg' => '注册上限',
		);
	}elseif(preg_match($pattern,$mail)==false){
		$arr = array(
			'code' => 0,
			'msg' => '邮箱格式错误',
		);
	}elseif($DB->get_row("SELECT * FROM `". DBQZ ."_user` where `mail` = '$mail' limit 1")){
		$arr = array(
			'code' => 0,
			'msg' => '邮箱已被绑定',
		);
	}elseif(strlen($qq) < 5){
		$arr = array(
			'code' => 0,
			'msg' => 'QQ号码格式错误',
		);
	}elseif($DB->get_row("SELECT * FROM `". DBQZ ."_user` where `qq` = '$qq' limit 1")){
		$arr = array(
			'code' => 0,
			'msg' => 'QQ号码已被绑定',
		);
	}elseif($DB->query("INSERT INTO `". DBQZ ."_user`(`user`, `pwd`, `qq`, `active`, `ip`, `regtime`, `regip`, `mail`, `state`) VALUES ('$user', '$pwd_md5', '$qq', '0', '$ip', '$date', '$ip', '$mail', '0')")){
		$arr = array(
			'code' => 1,
			'msg' => '注册成功',
		);
	}
	
	echo json_encode($arr); die;
}
?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8" />
		<title>注册 - <?=$conf['name']?></title>
		<link rel="icon" href="/favicon.ico" type="image/x-icon" />
		<meta name="description" content="" />
		<meta name="keywords" content="">
		<link rel="stylesheet" type="text/css" href="../layui/css/layui.css" />
		
		<style>
			body{
				margin: 0 auto;
				color: #2c3e50;
				/*
				text-align: center;
				*/
			}
			.container {
			  max-width: 480px;
			  padding: 50px;
			  margin: 0 auto;
			  text-align: center;
			}
		</style>
	</head>
<body style="background-image: url(../layui/images/background.png);">

	<div class="container">
		<div class="layui-col-md12">
			<div class="layui-card">
				<div class="layui-card-header">注册账号</div>
				<div class="layui-card-body">
					<form class="layui-form layui-form-pane" action="">
						<div class="layui-form-item">
							<label class="layui-form-label">账户</label>
							<div class="layui-input-block">
								<input type="text" id="user" autocomplete="off" placeholder="请输入账户名" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">密码</label>
							<div class="layui-input-block">
								<input type="password" id="pwd" autocomplete="off" placeholder="请输入密码" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">邮箱</label>
							<div class="layui-input-block">
								<input type="text" id="mail" autocomplete="off" placeholder="请输入邮箱" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">QQ号码</label>
							<div class="layui-input-block">
								<input type="text" id="qq" autocomplete="off" placeholder="请输入QQ号码" class="layui-input" oninput = "value=value.replace(/[^\d]/g,'')">
							</div>
						</div>
<?php if($conf['regcode']==1){ ?>
						<div class="layui-form-item">
							<label class="layui-form-label">验证码</label>
							<div class="layui-input-block">
								<input type="text" id="code" autocomplete="off" placeholder="请输入验证码" class="layui-input" style="padding-right: 102px;">
								<img id="img_code" style="position: absolute;top: 0;right: 1px; cursor: pointer;" src="../includes/code/get.code.php" onclick="$(this).attr('src','../includes/code/get.code.php?' + Math.random());">
							</div>
						</div>
<?php } ?>
						<button type="button" class="layui-btn layui-btn-fluid" onclick="reg()">注 册</button>
						<hr class="layui-bg-gray">
						<div class="" style="margin-left: 385px;">
							<a href="login.php" class="layui-btn layui-btn-primary layui-btn-xs">返回登录</a>
						</div>
					</form>
				</div>
			</div>
		</div>
	</div>
	
	<script src="../layui/layui.js"></script>
	<script src="../layui/layui.all.js"></script>
	<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
	<!--您的Layui代码start-->
	<script>
		$(document).keyup(function(event){
			if(event.keyCode ==13){
				reg();
			}
		});
		
		function reg(){
			var user 	= $('#user').val();
			var pwd  	= $('#pwd').val();
			var mail  	= $('#mail').val();
			var qq  	= $('#qq').val();
			var code  	= $('#code').val();
			if(user.length < 4){
				layer.msg('账户名长度小于4位',{icon: 5});
			}else if(pwd.length < 6){
				layer.msg('密码长度小于6位',{icon: 5});
			}else if(!mail.match(/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/)){
				layer.msg('邮箱格式错误',{icon: 5});
			}else if(qq.length < 5){
				layer.msg('QQ号码格式错误',{icon: 5});
			}else if(code==''){
				layer.msg('请输入验证码',{icon: 5});
			}else{
				var index = layer.load(1, {shade: [0.6,'#000']}); //0.1透明度的白色背景
				$.ajax({
					type: "post",   
					url:  "reg.php", 
					data: "from=reg&user=" + user + "&pwd=" + pwd + "&mail=" + mail + "&qq=" + qq + "&code=" + code,
					dataType: "json",
					success: function (res) {
						layer.close(index);
						if(res.code == 1){
							layer.msg(res.msg,{icon: 6});
							setTimeout('open_login()',2000);
						}else if(res.code == 0){
							layer.msg(res.msg,{icon: 5});
						}else if(res.code == 2){
							layer.msg(res.msg,{icon: 5});
							$('#img_code').attr('src','../includes/code/get.code.php?' + Math.random());
						}
					},
					error: function (res) {
						layer.msg('请求失败',{icon: 5});
						layer.close(index);
					}   
				});
			}
		}
		
		function open_login(){
			location.href = 'login.php'
		}
	</script>
</body>
</html>